package cn.jekun.shop.app.action;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;

import cn.jekun.component.base.bean.ErrorMsg;
import cn.jekun.component.base.exceptions.BusinessException;
import cn.jekun.shop.common.service.CartService;
import cn.jekun.shop.common.service.GoodsService;
import net.sf.json.JSONObject;

/**
 * 商品展示
 * @author twj
 *
 */
@SuppressWarnings("serial")
public class GoodsAction extends AppBaseAction{	

	@Resource(name = GoodsService.BEAN_NAME)
	protected GoodsService goodsService;
	
	@Resource(name = CartService.BEAN_NAME)
	protected CartService cartService;
	/**
	 * 查询商家商品列表
	 * @return
	 */
	public String queryShopGoodsList(){
		JSONObject jsonObject = null;
		try{
			String pageSize = this.request.getParameter("pageSize");//页大小
			String currentPage = this.request.getParameter("currentPage");//当前页
			String goodType = this.request.getParameter("goodType");//商品类型
			String shopId = this.request.getParameter("shopId");//商户编码
			String goodName = this.request.getParameter("goodName");//商品名称
			String barCode = this.request.getParameter("barCode");//商品条形码
			this.validateSign(pageSize,currentPage,goodType,shopId,goodName,barCode);
			jsonObject = goodsService.queryShopGoodsList(pageSize, currentPage, goodType, shopId, goodName,barCode);
			//购物车数量
			String userId = this.getUserId();
			if (StringUtils.isNotBlank(userId)) {
				JSONObject cartNums = cartService.getCartShopNums(userId, null,null);
				jsonObject.put("cartNums", cartNums.getInt("nums"));
			}else{
				jsonObject.put("cartNums", 0);
			}
		} catch (BusinessException e) {
			// TODO: handle exception
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
	}
	/**
	 * 商品列表
	 * @return
	 */
	public String goodsList(){
		JSONObject jsonObject = null;
		try{
			String pageSize = this.request.getParameter("pageSize");//页大小
			String currentPage = this.request.getParameter("currentPage");//当前页
			String goodType = this.request.getParameter("goodType");//商品类型
			String regionId = this.request.getParameter("regionId");//地区编码
			String distance = this.request.getParameter("distance");//距离
			String lng = this.request.getParameter("lng");//经度
			String lat = this.request.getParameter("lat");//纬度
			String shopId = this.request.getParameter("shopId");//商户编码
			String cityId = this.request.getParameter("cityId");//已开通城市代码
			String shopType = this.request.getParameter("shopType");//商家分类类型
			String sort = this.request.getParameter("sort");//排序字段
			String goodName = this.request.getParameter("goodName");//商品名称
			String barCode = this.request.getParameter("barCode");//商品条形码
			this.validateSign(""+pageSize+"",""+currentPage+"",goodType,regionId,distance,lng,lat,shopId,cityId,shopType,sort,goodName,barCode);
			jsonObject = goodsService.queryGoods(pageSize, currentPage, goodType, shopType, cityId, regionId, distance, lng, lat, shopId, sort, goodName,barCode,"","");
			//购物车数量
			String userId = this.getUserId();
			if (StringUtils.isNotBlank(userId)) {
				JSONObject cartNums = cartService.getCartShopNums(userId, null,null);
				jsonObject.put("cartNums", cartNums.getInt("nums"));
			}else{
				jsonObject.put("cartNums", 0);
			}
		} catch (BusinessException e) {
			// TODO: handle exception
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
	}
	
	/**
	 * 商品详情(app)
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String queryDetails() {
		JSONObject jsonObject = null;
		try {
			
			//验证签名是否正确 md5(merchandiseId,shopId)
			this.validateSign(request.getParameter("merchandiseId"));
			
			formMap.put("merchandiseId", request.getParameter("merchandiseId"));
			formMap.put("username", request.getParameter("username"));
			formMap.put("userId",request.getParameter("userId"));
			jsonObject = goodsService.querydetails(formMap);
			
			//购物车数量
			String userId = this.getUserId();
			if (StringUtils.isNotBlank(userId)) {
				JSONObject cartNums = cartService.getCartShopNums(userId, null,null);
				jsonObject.put("cartNums", cartNums.getInt("nums"));
			}else{
				jsonObject.put("cartNums", 0);
			}
			
		} catch (BusinessException e) {
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
	}

}
